package devyouk.util.excel.cellAdapter;

import org.apache.poi.ss.usermodel.CellType;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class CellValueAdapter<From, To> {

    private Class argumentType;

    public abstract CellType getCellType();

    public abstract To getCellValue(From data);

    public Class getCellValueType() {
        if (argumentType == null) {
            argumentType = getActualType();
        }
        return argumentType;
    }

    private Class getActualType() {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        try {
            Type typeArgument = type.getActualTypeArguments()[1];
            return Class.forName(typeArgument.getTypeName());
        } catch (ClassNotFoundException | IndexOutOfBoundsException e) {
            return String.class;
        }
    }
}
